package com.asiainfo.lop;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.asiainfo.lop.net.VolleyInterface;
import com.asiainfo.lop.net.VolleyRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 消息Fragment
 */
@EFragment(R.layout.main_fragment_message)
public class MainFragmentMessage extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @ViewById(R.id.main_fragment_message_reflash_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    @ViewById(R.id.main_crowd_attent_num)
    TextView attentNum;
    @ViewById(R.id.main_crowd_enter_num)
    TextView enterNum;
    @ViewById(R.id.main_crowd_area_num)
    TextView areaNum;

    @ViewById(R.id.main_crowd_attent_info)
    TextView attentInfo;
    @ViewById(R.id.main_crowd_enter_info)
    TextView enterInfo;
    @ViewById(R.id.main_crowd_area_info)
    TextView areaInfo;

    @ViewById(R.id.main_crowd_attent_time)
    TextView attentTime;
    @ViewById(R.id.main_crowd_enter_time)
    TextView enterTime;
    @ViewById(R.id.main_crowd_area_time)
    TextView areaTime;

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @AfterViews
    void init() {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        refreshData();
    }

    void refreshData() {
        String url = getString(R.string.rest_url_rooturl) + getString(R.string.rest_url_message_overview);

        VolleyInterface volleyInterface = new VolleyInterface(getActivity()) {
            @Override
            public void success(JSONObject result) {
                swipeRefreshLayout.setRefreshing(false);

                flag = result.getString("flag");
                reason = result.getString("reason");

                if (!RestResultInfo.RESULT_SUCCESS_FLAG.equals(flag)) {
                    Toast.makeText(getActivity(), reason, Toast.LENGTH_LONG).show();
                    return;
                }

                loadData(result.getJSONObject("data").getJSONArray("result"));
            }

            @Override
            public void error(VolleyError volleyError) {
                swipeRefreshLayout.setRefreshing(false);
            }
        };
        VolleyRequest.get(getContext(), url, "MainFragmentMessage", volleyInterface);
    }

    @Click(R.id.main_message_attent)
    void crowdAttentDetailBtnClick() {
        Intent intent = new Intent(getActivity(), MessageDetailListActivity_.class);
        intent.putExtra("type", "attent");
        startActivity(intent);
    }

    @Click(R.id.main_message_enter)
    void crowdEnterDetailBtnClick() {
        Intent intent = new Intent(getActivity(), MessageDetailListActivity_.class);
        intent.putExtra("type", "enter");
        startActivity(intent);
    }

    @Click(R.id.main_message_area)
    void crowdAreaDetailBtnClick() {
        Intent intent = new Intent(getActivity(), MessageDetailListActivity_.class);
        intent.putExtra("type", "area");
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    /**
     * 加载数据
     *
     * @param data
     */
    void loadData(JSONArray data) {
        for (int i = 0; i < data.size(); i++) {
            JSONObject j = data.getJSONObject(i);
            String warnTypeId = j.getString("warnTypeId");
            String warnInfo = j.getString("warnInfo");
            String warnTime = j.getString("warnTime");
            String warnNum = j.getString("warnNum");

            switch (warnTypeId) {
                case "1": {
                    attentNum.setText(warnNum);
                    attentInfo.setText(warnInfo);
                    attentTime.setText(warnTime);
                    break;
                }
                case "2": {
                    enterNum.setText(warnNum);
                    enterInfo.setText(warnInfo);
                    enterTime.setText(warnTime);
                    break;
                }
                case "3": {
                    areaNum.setText(warnNum);
                    areaInfo.setText(warnInfo);
                    areaTime.setText(warnTime);
                    break;
                }
            }

        }
    }
}
